package com.zhugang.week13.backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * @program algorithms
 * @description: subsets
 * @author: chanzhugang
 * @create: 2022/11/29 12:30
 */
public class Subsets {

    /**
     * 78 子集
     * https://leetcode.cn/problems/subsets/
     *
     * @param nums
     * @return
     */
    private List<List<Integer>> result = new ArrayList<>();

    public List<List<Integer>> subsets(int[] nums) {
        backtrack(nums, 0, new ArrayList<>());
        return result;
    }

    private void backtrack(int[] nums, int k, List<Integer> path) {
        if (k == nums.length) {
            result.add(new ArrayList<>(path));
            return;
        }

        // 不选当前阶段元素
        backtrack(nums, k + 1, path);
        // 选当前阶段元素
        path.add(nums[k]);
        backtrack(nums, k + 1, path);
        path.remove(path.size() - 1);
    }
}